package baseFx.database.mybatis.impl;

import baseFx.common.utils.StringUtils;
import baseFx.database.SqlParam;
import baseFx.database.mybatis.AbstractMybatisSql;
import baseFx.database.mybatis.MybatisContext;
import baseFx.database.mybatis.SqlPager;
import baseFx.database.query.PageSet;

public class MysqlSql extends AbstractMybatisSql {
    public MysqlSql(MybatisContext context) {
        super(context);
    }

    @Override
    protected SqlPager getPager(PageSet pageSet) {
        return new SqlPager() {
            private PageSet ps = pageSet;

            @Override
            public String get(String sql) {
                sql = StringUtils.trimRight(sql, ";");
                String newSql = "select count(1) c from(" + sql + ") t;";
                newSql += sql + " limit " + (ps.getStartRow() - 1) + "," + ps.pageSize + ";";
                return newSql;
            }
        };
    }

    @Override
    protected <T> void onPager(Class<T> clazz, PageSet pageSet, SqlParam param) {
        param.command = getPager(pageSet).get(param.command);
    }

    @Override
    public String escapeField(String field) {
        return "`" + field + "`";
    }
}
